import type { NextApiResponse } from 'next';
import * as categoryService from '@/common/service/g_category';
import R from '@common/type/response';
import { AuthenticatedRequest, withAuth } from '@common/middleware/auth';

export async function handler(req: AuthenticatedRequest, res: NextApiResponse) {
  const { id } = req.query;
  if (!id) {
    res.status(400).json({ success: false, message: '缺少ID' });
    return;
  }
  if (req.method === 'GET') {
    const data = await categoryService.getCategoryById(Number(id));
    R.success(res, data);
  } else if (req.method === 'PUT' || req.method === 'PATCH') {
    const affected = await categoryService.updateCategory(Number(id), req.body);
    R.success(res, affected);
  } else if (req.method === 'DELETE') {
    const affected = await categoryService.deleteCategory(Number(id));
    R.success(res, affected);
  } else {
    res.status(405).json({ success: false, message: 'Method Not Allowed' });
  }
} 

export default withAuth(handler,{
  GET:'project:category:query',
  PUT:'project:category:update',
  PATCH:'project:category:update',
  DELETE:'project:category:remove'
}, {
  PUT:{
    title:'分类',
    action:'UPDATE'
  }
});